package com.example;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;

import com.step.mesh.Color;
import com.step.mesh.buffer.ColorBuffer;
import com.step.mesh.buffer.CoordinateBuffer;
import com.step.mesh.buffer.FaceBuffer;

public class BufferExample extends Activity implements Renderer{
	GLSurfaceView glSurfaceView;
	//  立方体
	CoordinateBuffer positionBuffer = new CoordinateBuffer(24, 3);
	ColorBuffer colorBuffer = new ColorBuffer(24);
	CoordinateBuffer uvBuffer = new CoordinateBuffer(24, 2);
	CoordinateBuffer nomalBuffer = new CoordinateBuffer(24, 3);
	FaceBuffer faceBuffer = new FaceBuffer(48);
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		glSurfaceView = new GLSurfaceView(this);
		glSurfaceView.setRenderer(this);
		setContentView(glSurfaceView);
		
		// 坐标
		positionBuffer.add(-0.5f, -0.5f, 0.5f);
		positionBuffer.add(0.5f, -0.5f, 0.5f);
		positionBuffer.add(0.5f, 0.5f, 0.5f);
		positionBuffer.add(-0.5f, 0.5f, 0.5f);
		
		positionBuffer.add(0.5f, -0.5f, 0.5f);
		positionBuffer.add(0.5f, -0.5f, -0.5f);
		positionBuffer.add(0.5f, 0.5f, -0.5f);
		positionBuffer.add(0.5f, 0.5f, 0.5f);
		
		positionBuffer.add(0.5f, -0.5f, -0.5f);
		positionBuffer.add(-0.5f, -0.5f, -0.5f);
		positionBuffer.add(-0.5f, 0.5f, -0.5f);
		positionBuffer.add(0.5f, 0.5f, -0.5f);
		
		positionBuffer.add(-0.5f, -0.5f, -0.5f);
		positionBuffer.add(-0.5f, -0.5f, 0.5f);		
		positionBuffer.add(-0.5f, 0.5f, 0.5f);
		positionBuffer.add(-0.5f, 0.5f, -0.5f);
		
		positionBuffer.add(-0.5f, 0.5f, 0.5f);
		positionBuffer.add(0.5f, 0.5f, 0.5f);		
		positionBuffer.add(0.5f, 0.5f, -0.5f);
		positionBuffer.add(-0.5f, 0.5f, -0.5f);
		
		positionBuffer.add(-0.5f, -0.5f, 0.5f);
		positionBuffer.add(0.5f, -0.5f, 0.5f);		
		positionBuffer.add(0.5f, -0.5f, -0.5f);
		positionBuffer.add(-0.5f, -0.5f, -0.5f);
		
		// 颜色
		colorBuffer.add(new Color(255, 0, 0, 255));
		colorBuffer.add(new Color(255, 0, 0, 255));
		colorBuffer.add(new Color(255, 0, 0, 255));
		colorBuffer.add(new Color(255, 0, 0, 255));
		
		colorBuffer.add(new Color(0, 255, 0, 255));
		colorBuffer.add(new Color(0, 255, 0, 255));
		colorBuffer.add(new Color(0, 255, 0, 255));
		colorBuffer.add(new Color(0, 255, 0, 255));
		
		colorBuffer.add(new Color(0, 0, 255, 255));
		colorBuffer.add(new Color(0, 0, 255, 255));
		colorBuffer.add(new Color(0, 0, 255, 255));
		colorBuffer.add(new Color(0, 0, 255, 255));
		
		colorBuffer.add(new Color(255, 255, 0, 255));
		colorBuffer.add(new Color(255, 255, 0, 255));
		colorBuffer.add(new Color(255, 255, 0, 255));
		colorBuffer.add(new Color(255, 255, 0, 255));
		
		colorBuffer.add(new Color(255, 0, 255, 255));
		colorBuffer.add(new Color(255, 0, 255, 255));
		colorBuffer.add(new Color(255, 0, 255, 255));
		colorBuffer.add(new Color(255, 0, 255, 255));
		
		colorBuffer.add(new Color(0, 255, 255, 255));
		colorBuffer.add(new Color(0, 255, 255, 255));
		colorBuffer.add(new Color(0, 255, 255, 255));
		colorBuffer.add(new Color(0, 255, 255, 255));
		
		// 纹理
		uvBuffer.add(0, 1);
		uvBuffer.add(1, 1);
		uvBuffer.add(1, 0);
		uvBuffer.add(0, 0);
		
		uvBuffer.add(0, 1);
		uvBuffer.add(1, 1);
		uvBuffer.add(1, 0);
		uvBuffer.add(0, 0);
		
		uvBuffer.add(0, 1);
		uvBuffer.add(1, 1);
		uvBuffer.add(1, 0);
		uvBuffer.add(0, 0);
		
		uvBuffer.add(0, 1);
		uvBuffer.add(1, 1);
		uvBuffer.add(1, 0);
		uvBuffer.add(0, 0);
		
		uvBuffer.add(0, 1);
		uvBuffer.add(1, 1);
		uvBuffer.add(1, 0);
		uvBuffer.add(0, 0);
		
		uvBuffer.add(0, 1);
		uvBuffer.add(1, 1);
		uvBuffer.add(1, 0);
		uvBuffer.add(0, 0);
		
		// 索引
		faceBuffer.add(0, 1, 3);
		faceBuffer.add(1, 2, 3);
		faceBuffer.add(4, 5, 7);
		faceBuffer.add(5, 6, 7);
		faceBuffer.add(8, 9, 11);
		faceBuffer.add(9, 10, 11);
		faceBuffer.add(12, 13, 15);
		faceBuffer.add(13, 14, 15);
		faceBuffer.add(16, 17, 19);
		faceBuffer.add(17, 18, 19);
		faceBuffer.add(20, 21, 23);
		faceBuffer.add(21, 22, 23);
	}

	float rotation = 1;
	
	@Override
	public void onDrawFrame(GL10 gl) {
		gl.glClearColor(1, 0, 0, 1);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		
		gl.glLoadIdentity();
		gl.glPushMatrix();
		gl.glTranslatef(0, 0, -0.5f);
		rotation += 0.5f;
		gl.glRotatef(rotation, 1, 0, 0);
		gl.glRotatef(rotation, 0, 1, 0);
		gl.glRotatef(rotation, 0, 0, 1);
		gl.glScalef(1.5f, 1.5f, 1.5f);
		
		//gl.glTranslatef(-1.5f, 0, -1.0f);
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
		ByteBuffer color = colorBuffer.buffer();
		color.position(0);
		gl.glColorPointer(4, GL10.GL_UNSIGNED_BYTE, 0, color);		
		
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		FloatBuffer positions = positionBuffer.buffer();
		positions.position(0);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, positions);
		//gl.glDrawArrays(GL10.GL_TRIANGLES, 0, positionBuffer.size());
		ShortBuffer faces = faceBuffer.buffer();
		faces.position(0);
		gl.glDrawElements(GL10.GL_TRIANGLES, faceBuffer.size() * FaceBuffer.PROPERTIES_PER_ELEMENT,	GL10.GL_UNSIGNED_SHORT, faces);
		
		gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glPopMatrix();
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glOrthof(-width/2, width/2, -height/2, height/2, 1, -1);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();		
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// 启用阴影平滑
   gl.glShadeModel(GL10.GL_SMOOTH);
        
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glClearDepthf(1.0f);
		gl.glDepthFunc(GL10.GL_LESS);
		gl.glDepthRangef(0, 1f);
		gl.glDepthMask(true);
		
		// 告诉系统对透视进行修正
   gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
	}
}
